﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Avalonia.Controls;
using iOSClub.App.Desktop.Pages;
using iOSClub.App.Share;
using iOSClub.App.Share.WeatherModels;

namespace iOSClub.App.Desktop.ViewModels.Pages;

public class HomePageModel : PageModelBase
{
    public override Control Init()
    {
        Articles.Clear();
        IsArticlesLoad = false;
        IsLoad = false;
        return new HomePage() { DataContext = this };
    }
    
    public ObservableCollection<string> NavigationList { get; set; } = [];
    
    private MemberModel _user = new();

    public MemberModel User
    {
        get => _user;
        set
        {
            _user = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<Entry> Articles { get; set; } = [];

    private WeatherHour _now = new();

    public WeatherHour Now
    {
        get => _now;
        set => SetProperty(ref _now, value);
    }

    private WeatherWarning _warning = new();

    public WeatherWarning Warning
    {
        get => _warning;
        set => SetProperty(ref _warning, value);
    }

    public async Task Refresh()
    {
        IsLoad = false;
        var now = await Weather.GetNow(Weather.DefaultCity.id);
        if (now != null) Now = now;
        var warning = await Weather.GetWarning(Weather.DefaultCity.id);
        if (warning != null) Warning = warning;
        IsLoad = true;
    }

    private bool _isLoad;

    public bool IsLoad
    {
        get => _isLoad;
        set => SetProperty(ref _isLoad, value);
    }
    
    // Articles
    
    private bool _isArticlesLoad;

    public bool IsArticlesLoad
    {
        get => _isArticlesLoad;
        set => SetProperty(ref _isArticlesLoad, value);
    }
}